/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* p1, TreeNode* p2) {
        if(!p1 || !p2) return false;
        return IsSame(p1, p2) || HasSubtree(p1->left, p2) || HasSubtree(p1->right, p2);
    }
    bool IsSame(TreeNode* p1, TreeNode* p2) {
        if(!p2) return true;
        else if(!p1) return false;
        return p1->val==p2->val && IsSame(p1->left, p2->left) && IsSame(p1->right, p2->right);
    }
};